package org.hwadee.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.hwadee.entity.Comment;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CommentMapper {

    @Select("select * from comment where commentPostId = #{postId} and commentStatus = 1")
    List<Comment> listCommentByPostId(Integer postId);

    @Select("select * from comment where commentStatus = 1 order by commentCreateTime desc limit #{limit}")
    List<Comment> listRecentComment(int limit);

    @Select("select * from comment where commentPid = #{id} and commentStatus = 1")
    List<Comment> listChildComment(Integer id);

    @Select("select * from comment where commentUserId = #{userId} and commentStatus = 1 order by commentCreateTime desc limit #{i}")
    List<Comment> listRecentCommentById(@Param("userId")Integer userId, @Param("i")int i);

    @Insert("insert into comment(commentPid, commentPostId,  commentContent, commentCreateTime, commentUserId) values (#{commentPid}, #{commentPostId}, #{commentContent}, #{commentCreateTime}, #{commentUserId})")
    void insertComment(Comment comment);

    @Select("select count * from comment where commentPostId = #{commentPostId} and commentStatus = 1")
    Integer countCommentByPostId(Integer commentPostId);

    @Select("select * from comment where commentUserId = #{userId} and commentStatus = 1")
    List<Comment> getCommentsByCommentUserId(Integer userId);

    @Select("select * from comment where commentUserId = #{userId} and commentStatus = 1")
    List<Comment> getAllComments();

    @Select("select * from comment where commentPid in #{commentId} and commentStatus = 1")
    List<Comment> getReceiveCommentByUserId(Integer userId, List<Integer> commentId);

    @Select("select commentId from comment where commentUserId = #{userId} and commentStatus = 1")
    List<Integer> getCommentIdByUserId(Integer userId);

    @Select("select * from comment where commentId = #{id} and commentStatus = 1")
    Comment getCommentByCommentId(Integer id);

    @Update("update comment set commentStatus = 0 where commentId = #{id}")
    void deleteCommentByCommentId(Integer id);

    @Update("update comment set commentContent = #{commentContent} where commentId = #{commentId}")
    void updateComment(Comment comment);

    @Select("select * from comment where commentPostId = #{commentPostId} and commentStatus = 1")
    List<Comment> getCommentByPostId(Integer commentPostId);

    @Update("update comment set commentStatus = 0 where commentPostId = #{id}")
    void deleteCommentByCommentPostId(Integer id);

    @Update("update comment set commentStatus = 1 where commentPostId = #{id}")
    void recoveryCommentByCommentPostId(Integer id);

    @Select("select commentPostId from comment where commentId = #{id}")
    Integer getPostIdByCommentId(Integer id);
}
